package com.ott.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;

public class ScriptRunnerExecSql {

    public static void execSql(String filePath) {
        try {
            Connection conn =JDBCHelp.getConn();
            ScriptRunner runner = new ScriptRunner(conn);
            Resources.setCharset(Charset.forName("UTF-8")); //设置字符集
            runner.setLogWriter(null);//设置是否输出日志
            //IndexForm.textArea.append("开始数据库备份\r\n");
            Runtime r = Runtime.getRuntime();
            r.exec("chmod -R 755 /opt/initver/*");
            File file = new File("/opt/initver/");
            if (!file.exists()) {
				file.mkdir();
			}
            Process p  = r.exec("sh /opt/initver/dumptms.sh");
            if (p.waitFor() == 0) {
            	//IndexForm.textArea.append("数据库备份成功，开始执行sql脚本\r\n");
            	new ExecShell().execShell("/opt/initver/excue.sh "+filePath);
                //IndexForm.textArea.append("sql脚本执行成功\r\n");
			}else {
				//IndexForm.textArea.append("数据库备份失败，sql脚本执行失败\r\n");
			}
            

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
    	
    	Runtime r = Runtime.getRuntime();
	
			try {
				Process p = r.exec("bash C:/Users/admin/Desktop/excue.sh"+" C:/Users/admin/Desktop/up.sql");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//    	 Runtime r = Runtime.getRuntime();
//    	 
//    	 try {
//			r.exec("mysql -uroot -proot --default-character-set=utf8 tms < C:/Users/admin/Desktop/up.sql");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
   
}
